<script>
import {getAlerts} from "@/api/alertApi.js";
import {formatTime, toUtc} from "@/utils/datetimeTools.js";

export default {
  name: "AlertTable",
  data() {
    return {
      isLoading: true,
      altList: [],
    }
  },
  methods: {
    rowKey: (row) => row.alertId,
    formatErr: (row, column, cellValue, index) => cellValue ? (cellValue / 60).toFixed(4) : '',
    formatRecvTime: (r, c, v, i) => formatTime(toUtc(v)),
    freshData: function () {
      this.isLoading = true;
      getAlerts().then((res) => {
        // console.log(res);
        this.isLoading = false;
        this.altList = res;
      }).catch((err) => {
        this.isLoading = false;
      })
    }
  },
  mounted() {
    this.freshData();
  }
}
</script>

<template>
  <div>
    <div class="text-center">
      <h3>Alert</h3>
    </div>
    <hr/>
    <el-table
        class="table"
        :data="altList"
        v-loading="isLoading"
        :row-key="rowKey"
    >
      <el-table-column prop="alertId" align="center" label="ID"/>
      <el-table-column prop="triggerName" align="center" label="Trigger"/>
      <el-table-column prop="triggerTypeName" align="center" label="Type"/>
      <el-table-column prop="triggerTelescopeName" align="center" label="Telescope"/>
      <el-table-column prop="alertPktSerNum" align="center" label="PktSerNum"/>
      <el-table-column prop="triggerRa" align="center" label="RA (deg)"/>
      <el-table-column prop="triggerDec" align="center" label="DEC (deg)"/>
      <el-table-column prop="triggerErr" :formatter="formatErr" align="center" label="Error (arcmin)"/>
      <el-table-column prop="triggerTime" align="center" resizable label="Trigger Time (UT)"/>
      <el-table-column prop="sourceClassificationExplanation" align="center" label="Classification"/>
      <el-table-column prop="alertReversion" align="center" label="Reversion"/>
      <el-table-column prop="insertTime" :formatter="formatRecvTime" align="center" resizable label="Recv Time (UT)"/>
    </el-table>
  </div>

</template>

<style scoped>

</style>